package com.apes.framework.plugin.form.parser.element;

import com.apes.framework.plugin.filereader.xml.ApesElement;
import com.apes.framework.plugin.filereader.xml.ApesElementParser;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能：
 *
 * @author xul
 * @create 2018-04-16 15:58
 */
public abstract class AbstractElementParser implements ApesElementParser<ApesElement, Map> {

    @Override
    public Map parse(ApesElement element) {
        Map node = new HashMap();
        parseAttributes(node, element.getAttributes());
        parseNestedElements(node, element.getNestedElements());
        return node;
    }

    /**
     * 功能：解析属性元素
     */
    protected void parseAttributes(Map node, Map<String, String> attributes) {
        attributes.forEach((k, v) -> {
            node.put(k, v);
        });
    }

    /**
     * 功能：解析子节点
     */
    protected void parseNestedElements(Map node, Collection<ApesElement> elements) {
        elements.forEach((apesElement) -> {
            node.put(apesElement.getName(), apesElement.parse());
        });
    }

}
